#*****************************************************************************************************
#* FILE: pointers.asm
#*
#* DECRIPTION
#* Write a program that will compute the sum of all the elements in an array.
#*
#* AUTHORS
#* Joe Boeding (Joe.Boeding@asu.edu) [JB]
#* Taylor Wood (Taylor.D.Wood@asu.edu) [TW]
#*
#* Computer Science & Engineering
#* CSE420 Computer Architecture I, Spring 2014
#*
#* MODIFICATION HISTORY
#* --------------------------------------------------------------------------------------------------
#****************************************************************************************************
# compute the sum of elements in an array

.data
array: .word   0 : 10        # "array" of 10 words
size: .word  10             # size of "array" + 1
sum: .word 0			# initial sum is 0
      .text 

li $t0, 0	# t0 represents i
lb $t1, size	# size of the array
la $s0, array	# the array
la $s1, sum	# pointer to the sum 

# function to insert values into the array
createArray:

	sw $t0, 0($s0)		# store the value of i in array
	addi $t0, $t0, 1	# increment i
	addi $s0, $s0, 4	# increment to next position in array
	blt $t0, $t1, createArray
	
li $t0, 0	# i = 0
la $t2, array	# pointer to current element
subi $t2, $t2, 4	# go to previous element since we are passing a+i

loop:
	move $a0, $s1	# pointer to the running sum
	addi $t2, $t2, 4	# a + i
	move $a1, $t2	# pointer to the current element in the array
	jal PSum
	addi $t0, $t0, 1	# i++
	blt $t0, $t1, loop

lw $a0, 0($s1)
li $v0, 1	# print the sum
syscall


li $v0, 10  # Syscall number 10 is to terminate the program
syscall     # exit now

PSum:

	lw $t6, 0($a0)	# get the running sum
	lw $t7, 0($a1)	# get the current element
	
	add $t6, $t6, $t7	# s += e
	
	sw $t6, 0($a0)	# store the sum
	sw $t7, 0($a1)	# store the current element
	
	jr $ra